//
//  ViewController.swift
//  表情键盘
//
//  Created by Apple on 16/7/28.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var textView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        textView.becomeFirstResponder()
        
        // 表情键盘其实就是一个UIView
        textView.inputView = emoticonKeyBoard
        
//        testEmoji()
        
        let packages = HMEMoticonManager.sharedManager.emoticonPackages
//        print("packages: \(packages)")
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let text = textView.emoticonText()
        print("text: \(text)")
    }
    
    private func testEmoji() {
        // 16进制字符串 -> 转emoji
        let code = "0x1f603"
        
        // 扫描
        let sacnner = NSScanner(string: code)
        
        var result: UInt32 = 0
        // UnsafeMutablePointer<UInt32>: 要一个UInt32类型的指针
        sacnner.scanHexInt(&result)
        print("result: \(result)")
        
        let scalar = UnicodeScalar(result)
        
        let c = Character(scalar)
        let emoji = String(c)
        print("emoji: \(emoji)")
        
        textView.text = emoji
    }
    
    private lazy var emoticonKeyBoard: HMEmoticonKeyboard = {
        let keyboard = HMEmoticonKeyboard()
        
        keyboard.textView = self.textView
        
        return keyboard
    }()
}

